package com.eningqu.aipen.ui.adapter;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.eningqu.aipen.MyApplication;
import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ItemWorkBinding;
import com.eningqu.aipen.logic.model.medical.GhTodayBean;
import com.eningqu.aipen.logic.network.Apis;
import com.eningqu.aipen.logic.network.BaseRetrofitUtils;
import com.eningqu.aipen.logic.network.SchedulerTransformer;
import com.eningqu.aipen.ui.home.book.WriteActivity;
import com.eningqu.aipen.utils.GlideUtils;
import com.eningqu.aipen.utils.PopTipUtils;
import com.eningqu.aipen.utils.ToastUtils;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;

import java.util.List;

/**
 * Package:com.eningqu.aipen.ui.adapter
 * Author:starr
 * Time:2023/11/19 18:52
 * Description: 主页病历本适配器
 */
public class WorkAdapter extends RecyclerView.Adapter<WorkAdapter.WorkViewHolder> {


    private final List<GhTodayBean.GhListDTO> list;
    public WorkAdapter(List<GhTodayBean.GhListDTO> list, ObservableBoolean isConnect) {
        this.list = list;
    }

    @NonNull
    @Override
    public WorkViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemWorkBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_work, viewGroup, false);
        return new WorkViewHolder(binding.getRoot());
    }

    @SuppressLint({"SetTextI18n", "CheckResult"})
    @Override
    public void onBindViewHolder(WorkViewHolder holder, int i) {


        GhTodayBean.GhListDTO bean = list.get(i);

        new Thread(() -> {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.sendMessage(new Message());
        }).start();

        if (bean.emrGhState == 0) {
            Glide.with(holder.itemView).load(R.drawable.ic_undeal).into(holder.binding.ivDeal);
            holder.binding.tvDeal.setText("待就诊");
        } else {
            Glide.with(holder.itemView).load(R.drawable.ic_deal).into(holder.binding.ivDeal);
            holder.binding.tvDeal.setText("已就诊");
        }
        GlideUtils.loadCircleImage(holder.itemView.getContext(), bean.hzInfoEntity.emrHzImg, holder.binding.ivHead);

        holder.binding.ivMyBook.setOnClickListener(v -> {
            if (MyApplication.isConnect == -1) {
                ToastUtils.showToast(v.getContext(), "请先连接智能点阵笔....");
            } else if (bean.emrGhState == 1) {
                PopTipUtils.warnMsg("当前患者已就诊");
            } else {
                BottomMenu.show(new String[]{"西医病历本(支持手写笔)", "中医病历本(支持手写笔)"})
                        .setMessage("请选择病历模板")
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu bottomMenu, CharSequence charSequence, int i) {
                                if (i >= 2) {
                                    com.luck.picture.lib.utils.ToastUtils.showToast(v.getContext(), charSequence.toString() + "  " + i);
                                } else {
                                    Bundle bundle = new Bundle();
                                    BaseRetrofitUtils.getInstance().create(Apis.class).getBlByHzId(bean.emrHzId)
                                            .compose(new SchedulerTransformer<>())
                                            .subscribe(bookBean -> {
                                                bundle.putInt("blId", bookBean.blEntity.emrBlId);
                                                bundle.putInt("ghId", bean.emrGhId);
                                                bundle.putInt("hzId", bean.emrHzId);
                                                bundle.putInt("blModel", i);
                                                ToastUtils.showToast(v.getContext(), "患者id" + bean.emrHzId + "  挂号id:" + bean.emrGhId);
                                                WriteActivity.startAction(v.getContext(), bundle);
                                            });
                                }
                                return false;
                            }
                        });

            }
        });
        holder.binding.tvName.setText(bean.hzInfoEntity.emrHzName);
        holder.binding.tvDate.setText(bean.emrGhTime.substring(0, 11) + "\n" + bean.emrGhTime.substring(11));

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    protected static class WorkViewHolder extends RecyclerView.ViewHolder {

        ItemWorkBinding binding;

        public WorkViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.getBinding(itemView);
        }
    }
}
